package com.shuyihui.databindinglib;


import android.view.LayoutInflater;

import androidx.databinding.ViewDataBinding;
import androidx.viewbinding.ViewBinding;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;

/**
 * 创建日期 2020/10/19
 * 描述：只使用 DataBind 不使用ViewModel
 */
public abstract class BaseDActivity<VDB extends ViewDataBinding> extends BaseActivity{
    protected VDB mBinding;

    @Override
    protected void initContentView() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class cls = (Class) type.getActualTypeArguments()[0];
        try {
            Method inflate = cls.getDeclaredMethod("inflate", LayoutInflater.class);
            mBinding = (VDB) inflate.invoke(null, getLayoutInflater());
            mBinding.setLifecycleOwner(this);
            setContentView(mBinding.getRoot());
        } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getContentLayout() {
        return 0;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBinding!= null){
            mBinding.unbind();
            mBinding = null;
        }
    }
}
